//
//  OderSectionFooterView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/22.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "OderSectionFooterView.h"
@interface OderSectionFooterView()

@property(nonatomic,strong)UIImageView * backGroundimage;

/**"共"*/
@property(nonatomic,strong)UILabel * l1;

/**"件商品"*/
@property(nonatomic,strong)UILabel * l2;

/**"实付"*/
@property(nonatomic,strong)UILabel * l3;
@end


@implementation OderSectionFooterView
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        UIImageView * imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brokerage_bg02"]];
        [self insertSubview:imgv atIndex:0];
        self.backGroundimage = imgv;
        
        UILabel * l1 = [UILabel labelWithTextColor:[UIColor blackColor] font:[UIFont systemFontOfSize:12]];
        l1.text = @"共 ";
        [self addSubview:l1];
        UILabel * l2 = [UILabel labelWithTextColor:[UIColor blackColor] font:[UIFont systemFontOfSize:12]];
        l2.text = @" 件商品";
        [self addSubview:l2];
        
        UILabel * l3 = [UILabel labelWithTextColor:[UIColor blackColor] font:[UIFont systemFontOfSize:12]];
        l3.text = @"实付 ";
        [self addSubview:l3];
        
        self.l1 = l1;
        self.l2 = l2;
        self.l3 = l3;
        
        UILabel *  goodsLabel = [UILabel labelWithTextColor:RGBColor(232,145,33) font:[UIFont systemFontOfSize:12]];
        [self addSubview:goodsLabel];
        
        UILabel * priceLabel = [UILabel labelWithTextColor:RGBColor(232,145,33) font:[UIFont systemFontOfSize:12]];
        [self addSubview:priceLabel];
        
        self.goodsLabel = goodsLabel;
        self.priceLabel = priceLabel;
        
        UIButton * optionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [optionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [optionButton addRadius:5];
        optionButton.titleLabel.font = [UIFont systemFontOfSize:12];
        optionButton.backgroundColor = RGBColor(232,145,33);
        [self addSubview:optionButton];
        [optionButton addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.optionButton = optionButton;
        
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.backGroundimage.x = globalMargin;
    self.backGroundimage.height = self.height - globalMargin;
    self.backGroundimage.y = 0;
    self.backGroundimage.width = ScreenWidth - 2*globalMargin;
    
    self.priceLabel.x = self.width - self.priceLabel.width - 2*globalMargin;
    self.priceLabel.y = globalMargin;
    
    self.l3.size = [self.l3.text sizeInAre:MaxSize font:self.l3.font];
    self.l3.x = self.priceLabel.x - self.l3.width;
    self.l3.centerY = self.priceLabel.centerY;
    
    self.l2.size = [self.l2.text sizeInAre:MaxSize font:self.l2.font];
    self.l2.x = self.l3.x - self.l2.width - globalMargin;
    self.l2.centerY = self.l3.centerY;
    
    self.goodsLabel.size = [self.goodsLabel.text sizeInAre:MaxSize font:self.goodsLabel.font];
    self.goodsLabel.x = self.l2.x - self.goodsLabel.width;
    self.goodsLabel.centerY = self.l2.centerY;
    
    self.l1.size = [self.l1.text sizeInAre:MaxSize font:self.l1.font];
    self.l1.x = self.goodsLabel.x - self.l1.width;
    self.l1.centerY = self.goodsLabel.centerY;
    
    self.optionButton.size = CGSizeMake(MineRatio(83), self.height - 2*globalMargin - self.priceLabel.botton-4);
    self.optionButton.x = self.width - self.optionButton.width - 2*globalMargin;
    self.optionButton.y = self.height - 2*globalMargin - self.optionButton.height;
}

-(void)setPrice:(CGFloat)price{
    NSString * str = [NSString stringWithFormat:@"¥%.2f",price];
    NSArray * sta = [str componentsSeparatedByString:@"."];
    NSMutableAttributedString * attstr = [[NSMutableAttributedString alloc] initWithString:str];
    if(sta.count>1){
        [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 1)];
        [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(1, [sta.firstObject length]-1)];
        [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange([sta.firstObject length], attstr.length-[sta.firstObject length])];
    }else{
        [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, attstr.length)];
    }
    
    self.priceLabel.attributedText = attstr;
    self.priceLabel.size = attstr.size;
    [self setNeedsLayout];
}

-(void)optionButtonClick:(UIButton*)button{
    if(self.delegate&&[self.delegate respondsToSelector:@selector(OderSectionFooterView:didClickOptionButton:)]){
        [self.delegate OderSectionFooterView:self didClickOptionButton:button];
    }
}

@end
